﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using newtelligence.DasBlog.Runtime;
using dasBlog.Storage;

namespace dasBlog.Services.Interop
{
    class StorageBusLoggingService : ILoggingDataService, ILoggingDataServiceInitialize
    {
        private Moniker scopeUri;
        private Moniker eventsUri;
        private Moniker logUri;

        public StorageBusLoggingService()
        {
         
        }

        public StorageBusLoggingService(string scopeName)
        {
            Initialize(scopeName);
        }

        public void Initialize(string scopeName)
        {
            this.scopeUri = Moniker.FromScopeName(scopeName);
            this.eventsUri = new Moniker(scopeName, PathSegmentName.Events);
            this.logUri = new Moniker(scopeName, PathSegmentName.Trackings);
        }


        public LogDataItemCollection GetReferralsForDay(DateTime date)
        {
            return GetLogItems(date, "Referral");
        }
        
        public void AddReferral(LogDataItem logItem)
        {
            AddLogItem(logItem, "Referral");
        }
                
        public LogDataItemCollection GetClickThroughsForDay(DateTime date)
        {
            return GetLogItems(date, "Clickthrough");
        }

        public void AddClickThrough(LogDataItem logItem)
        {
            AddLogItem(logItem, "Clickthrough");
        }

        public LogDataItemCollection GetAggregatorBugHitsForDay(DateTime date)
        {
            return GetLogItems(date, "Aggbug");
        }

        public void AddAggregatorBugHit(LogDataItem logItem)
        {
            AddLogItem(logItem, "Aggbug");
        }

        public LogDataItemCollection GetCrosspostReferrersForDay(DateTime date)
        {
            return GetLogItems(date, "Crosspost");
        }

        public void AddCrosspostReferrer(LogDataItem logItem)
        {
            AddLogItem(logItem, "Crosspost");
        }

        public EventDataItemCollection GetEventsForDay(DateTime date)
        {
            return new EventDataItemCollection(
                (from entry in 
                      eventsUri.Select<EventEntry>(
                                        MonikerMatch.Exact,
                                        new DateRangeQueryDescription(date.Date, date.Date.AddDays(1).AddMilliseconds(-1)))
                 orderby entry.Time ascending
                 select new EventDataItem
                 {
                      EventCode = int.Parse(entry.Code),
                      EventTimeUtc = entry.Time,
                      HtmlMessage = entry.Message
                 }).ToArray());
        }

        public void AddEvent(EventDataItem eventData)
        {
            EventEntry logEntry = new EventEntry
            {
                 Code = eventData.EventCode.ToString(),
                 Message = eventData.HtmlMessage,
                 Time = eventData.EventTimeUtc
            };
            eventsUri.Store<EventEntry>(logEntry);
        }

        private void AddLogItem(LogDataItem logItem, string type)
        {
            LogEntry logEntry = new LogEntry
            {
                TrackingType = type,
                ReferrerUrl = logItem.UrlReferrer,
                UserAgent = logItem.UserAgent,
                UserDomain = logItem.UserDomain,
                UrlRequested = logItem.UrlRequested,
                RequestTime = logItem.RequestedUtc
            };
            logUri.Store<LogEntry>(logEntry);
        }

        private LogDataItemCollection GetLogItems(DateTime date, string type)
        {
            return new LogDataItemCollection(
                (from entry in 
                      logUri.Select<LogEntry>(
                                        MonikerMatch.Exact,
                                        new DateRangeAndTagQueryDescription(date.Date, date.Date.AddDays(1).AddMilliseconds(-1), type))
                 orderby entry.RequestTime ascending
                 select new LogDataItem
                 {
                      RequestedUtc = entry.RequestTime,
                      UrlReferrer = entry.ReferrerUrl,
                      UrlRequested = entry.UrlRequested,
                      UserAgent = entry.UserAgent,
                      UserDomain = entry.UserDomain
                 }).ToArray());
        }


    }
}
